"""
文件名: DHX_MemberInterface.py
作用: 录入会员模块的接口
作者: 代海霞
"""

from Untils.SendMethod import SendMethod
from Untils.GetKeyword import GetKeyword


class MemberInterface:
    def __init__(self):
        self.url = 'http://139.159.146.104:8083'

    def get_auth_code(self, telephone):
        """
        获取验证码接口
        :param telephone: 手机号
        :return:
        """
        # 请求地址
        url = self.url + "/sso/getAuthCode"
        # 请求数据
        payload = {'telephone': telephone}
        return SendMethod.send_method(method='get', url=url, params=payload)

    def __get_data(self, telephone):
        """
        获取验证码
        :param telephone: 手机号
        :return:验证码的值
        """
        return GetKeyword.get_keyword(self.get_auth_code(telephone), 'data')

    def member_register(self, username, password, telephone):
        """
        会员注册接口
        :param username: 用户名
        :param password: 用户密码
        :param telephone: 手机号
        :return:
        """
        # 请求地址
        url = self.url + "/sso/register"
        # 请求数据
        payload = {
            'username': username,
            'password': password,
            'telephone': telephone,
            'authCode': self.__get_data(telephone)
        }
        return SendMethod.send_method(method='post', url=url, data=payload)

    def member_login(self, username, password):
        """
        会员登录接口
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + "/sso/login"
        # 请求数据
        payload = {
            'username': username,
            'password': password
        }
        return SendMethod.send_method(method='post', url=url, data=payload)

    def get_token(self, username, password):
        """
        获取会员登录token
        :param username: 用户名
        :param password: 密码
        :return: 拼接后的token请求头信息
        """
        token = GetKeyword.get_keyword(self.member_login(username, password), 'token')
        return {"Authorization": f'Bearer {token}'}

    def refresh_token(self, username, password):
        """
        刷新token
        :return:
        """
        # 请求地址
        url = self.url + "/sso/refreshToken"
        return SendMethod.send_method(method='get', url=url, headers=self.get_token(username, password))

    def get_member_info(self, username, password):
        """
        获取会员信息
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + '/sso/info'
        headers = self.get_token(username=username, password=password)
        return SendMethod.send_method(method='get', url=url, headers=headers)

    def update_password(self, telephone, password):
        """
        更改用户密码
        :param telephone: 用户手机号
        :param password: 密码
        :return:
        """
        url = self.url + '/sso/updatePassword'
        payload = {
            'telephone': telephone,
            'password': password,
            'authCode': self.__get_data(telephone)
        }
        return SendMethod.send_method(method='post', url=url, data=payload)


if __name__ == '__main__':
    member = MemberInterface()
    # print(member.get_auth_code(telephone='18312345678'))
    # print(member.member_register(username='深红', password='123456', telephone='18212345678'))
    # print(member.member_login(username='liar', password='123456'))
    # print(member.get_token(username='liar', password='123456'))
    # print(member.refresh_token(username='liar', password='123456'))
    print(member.get_member_info(username='liar', password='123456'))
    # print(member.update_password(telephone='18212345678', password='123456'))
